/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.renren.common.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Map工具类
 *
 * @author Mark sunlightcs@gmail.com
 * @since 2.0.0
 */
@SuppressWarnings("rawtypes")
public class MapUtils extends HashMap<String, Object> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public MapUtils put(String key, Object value) {
		super.put(key, value);
		return this;
	}

	public static String toStr(Map map, String key) {
		if (map == null || map.get(key) == null) {
			return null;
		}
		return map.get(key).toString();
	}

	public static Integer toInt(Map map, String key) {
		if (map == null || map.get(key) == null) {
			return null;
		}
		try {
			return Integer.parseInt(map.get(key).toString());
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Long toLong(Map map, String key) {
		if (map == null || map.get(key) == null) {
			return null;
		}
		try {
			return Long.parseLong(map.get(key).toString());
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date toDate(Map map, String key) {
		if (map == null || map.get(key) == null) {
			return null;
		}
		try {
			try {
				return new Date(Long.parseLong(map.get(key).toString()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return null;
	}
}
